jQuery(window).load(function() {
	lazyloadIMG();
	// HOME P
	slideMenu();
	searchBlock();
	// PRO DETAIL p
	poupProd();
	// COMMENT P
	showReply();
	// fix css video
	getVideo();
});
/* END: js load */

function getVideo(){
	var objVideo = $('.main').find('object, iframe, embed');
	if(!objVideo.length){
		return;
	}else{
		$(objVideo).parent('p').addClass('video');
	}
}

/* END: function get video addClass video */

/*
 * for Mainmenu, it can slide left to right
 */
function slideMenu(){
	var widthMenu =	$('#menu').width();
	var heightMenu = $('#menu').height();	
	var blockSlide = $('.main').find('article').hasClass('slide-block');
	
	if(blockSlide == true){		
		$(".show-menu").live("click", function(){
			console.log(blockSlide);
			$('#menu').animate({		 
				right: '280px'		    
			  }, 300);
			//$(this).addClass('hideicon').removeClass('lanmenu');
			$('.close').fadeIn(500);
		});
		$("#menu .close").live("click", function(){
			$('#menu').animate({		 
				right: '-1px'		    
			  }, 60);
			// $('.show-menu').removeClass('hideicon').addClass('lanmenu');
			$(this).fadeOut(100);
		}); 		
	}else{
		$(".show-menu").live("click", function(){
			$('#menu').addClass('open');
			$(this).addClass('hideicon').removeClass('lanmenu');
			$('.close').fadeIn(800);
		});
		$("#menu .close").live("click", function(){
			$('#menu').removeAttr('class');
			$('.show-menu').removeClass('hideicon').addClass('lanmenu');
			$(this).fadeOut(500);
		});
	}	
}
/* END: function slideMenu */

/*
 * for search box. click search icon and show it.
 */
function searchBlock() {
	hideValFocus($('#search-txt'));	
	jQuery('.search').on('click', function(){
		var obj = $(this).attr('href');
		var that  = $(obj);
		if(!that.length){	return;	}
		
		if($(this).hasClass('search-close') == false){
			$(this).addClass('search-close');				
			$('header').animate({
				marginBottom: 50
			},200);						
			that.slideDown(300, function() {					
				// Animation complete.
				that.children('form').animate({
					opacity : 1
				},400);									
			});	
						
		}else{
			$(this).removeClass('search-close');	
			that.children('form').animate({
				opacity : 0
			} ,function() {
				// Animation complete.
				that.slideUp("fast", function() {
					// Animation complete.				
					$('header').animate({
						marginBottom: 0
					},300);
				});
			});	
		}
		return false;
	})
}

/*
 * for helping in hide value when focus input.
 */
function hideValFocus($obj_input) {
		var txtval = $obj_input.val();
		$obj_input.focus(function(){
			$obj_input.val('')
		});
		$obj_input.blur(function(){
			if($obj_input.val() == ""){
				$obj_input.val(txtval);
			}
		});	
}
/* END: function search-block */



/*
 * for Product detail page. click and show a popup
 */
function poupProd(){
	
	var heightScreen     = $(window).height();
	var heightTop        = $('.main').find('.detail-page').offset();
	
	if(!heightTop){	return; }	
	$('.ct-popup').css({
		//'height'  : heightScreen - heightTop.top - 20,
		'top'	  : heightTop.top-5,
		'display' : 'block'
	});
	$('.ranks-list').delegate('a', 'click', function(){		
		var obj = $(this).attr('title');
		var that  = $(obj);
			
		if( !that.length){		return; 	}
		var heightContentPop = that.find('.outer').height();
		
		if (heightContentPop > heightScreen){
			that.css({
				'height'  : heightScreen - heightTop.top - 20
			});
			that.find('.inner').css({
				'max-height'  : heightScreen - heightTop.top - 100
			});
		}		
		that.addClass('open').css({
			'right'		  : 2+'%',
		});
		that.find('.close').click(function(){
			that.removeClass('open').css({
				'right'		  : -4+'%'
			});
		});
		if(that.hasClass('ct-popup') == true){
			$('body,html').animate({
				scrollTop: $('header').offset().top
			}, 800);
		}		
		return false;		
	})
}
/* END: function poupProd */
function showReply() {
	jQuery('.read_reply').on('click', function(){
			$(this).next('.reply_list').toggle();
	});
}

/* LAZY IMAGE LOAD */
function lazyloadIMG() {
	if ($(".news-spec img").length) {
		$(".news-spec img").lazyload();
	}
	if ($(".prod-item figure img").length) {
		$(".prod-item figure img").lazyload();
	}	 
}



/* scroll to lib */
(function(a){a.fn.extend({scrollTopObj:function(b){var c={topHeight:"",objMove:""};var b=a.extend(c,b);return this.each(function(){var c=b;var d=a(this);var e=jQuery(c.objMove).offset();a("body,html").animate({scrollTop:e.top-c.topHeight},800);return false})}})})(jQuery)


